﻿using MediatR;
using T_Person.BaseRep.Interface;
using T_Person.Read.APi.Applaction.Command.TotalPersonnel;
using T_Person.Read.APi.Dto;

namespace T_Person.Read.APi.Applaction.CommandHandlers.TotalPersonnelHandlers
{
    public class TotalPersonnelByMonthHandlers : IRequestHandler<TotalPersonCountByMonthCommand, List<PersonnelAgeGroup>>
    {
        private readonly IPersonnelRep personnelRep;

        public TotalPersonnelByMonthHandlers(IPersonnelRep personnelRep)
        {
            this.personnelRep = personnelRep;
        }

        public async Task<List<PersonnelAgeGroup>> Handle(TotalPersonCountByMonthCommand request, CancellationToken cancellationToken)
        {
            var list = await personnelRep.GetvaluesByResult(x => !x.IsDel && x.RLZId.Equals(3) && x.AuditState.Equals(2));
            var listmonth = new List<PublicMonth>();
            for (int i = 1;i<=12;i++)
            {
                var list1 = new PublicMonth
                {
                    data = i,
                    month = i + "月"
                };
                listmonth.Add(list1);
            }
            var lists = new List<PersonnelAgeGroup>();
            foreach (var item in listmonth)
            {
                lists.Add(new PersonnelAgeGroup
                {
                    name = item.month,
                    number = list.Where(x=>x.CreateDate.Value.Month==item.data).Count()>0? list.Where(x => x.CreateDate.Value.Month == item.data).Count():0
                });
            }
            return lists;
        }
    }
}
